import java.util.*;
import java.util.function.BiConsumer;

/**
 * Created with IntelliJ IDEA.
 * Description: Map和 Set的学习代码
 */

//内部类的测试
class Demo {
    int num1 = 1;
    private int num2 = 2;
    static class Demo2 {
        int num3 = 3;
        Demo demo = new Demo();
        public void func() {
            System.out.println(demo.num1);
            System.out.println(demo.num2);
            System.out.println(num3);
        }
    }
}

public class Test {

    //实例化entrySet内部类
    public static void main6(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("hello", 1);
        map.put("world", 2);
        map.forEach((key,value)-> System.out.println("key: " + key + " value: " + value));
        Set<Map.Entry<String, Integer>> set = map.entrySet();
        for (Map.Entry<String, Integer> entry: set) {
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
        System.out.println();
        Iterator<Map.Entry<String, Integer>> iterator = set.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    //forEach方法，实现BiConsumer接口
    public static void main(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("hello", 1);
        map.put("world", 2);
        map.forEach(new BiConsumer<String, Integer>() {
            @Override
            public void accept(String s, Integer integer) {
                System.out.println("key: " + s + " value: " + integer);
            }
        });
        map.forEach((key,value)-> System.out.println("key: " + key + " value: " + value));
    }

    //ToString方法
    public static void main4(String[] args) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("hello", 1);
        map.put("world", 2);
        System.out.println(map);
    }

    //内部类的测试
    public static void main3(String[] args) {
        Demo.Demo2 demo2 = new Demo.Demo2();
        demo2.func();
    }

    public static void main2(String[] args) {
        TreeMap<String,Integer> map = new TreeMap<>();
        map.put("hello", 1);
        map.put("world", 2);
        Set<Map.Entry<String, Integer>> set = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = set.iterator();
        while(iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

    public static void main1(String[] args) {
        TreeMap<String,Integer> map = new TreeMap<>();
        //V put(K key,V value) | 增加key和对应的value或更改原有key的value值
        map.put("hello", 1);
        map.put("world", 2);
        System.out.println(map);
        //V get(Object key) | 返回key对应的value值
        System.out.println(map.get("world"));
        /**
        * V getOrdefault(Object key,V defaultValue) | 返回key对应的value，key不存在时，返回默认值
        */
        //注意，由于我们的value类型时Integer类型的，因此如果没有找到对应的key，并且采用int类型接收将极其危险
        //因为此时会return null，而null无法自动拆包，int无法接收，就会报空指针异常错误
        /*错误示范*/
        //int num = map.get("main");
        /*正确示范*/
        Integer num1 = map.get("main");
        System.out.println(num1);
        System.out.println(map.getOrDefault("main",0));
        //Set<K> keySet() | 返回所有key的不重复集合
        Set<String> set = map.keySet();
        System.out.println(set);
        //Collection<V> values()
        Collection<Integer> collection = map.values();
        System.out.println(collection);
        //Set<Map.Entry<K,V>>entrySet()
        Set<Map.Entry<String, Integer>> mapSet = map.entrySet();
        for (Map.Entry<String, Integer> entry: mapSet) {
            System.out.println("key:" + entry.getKey() + " value:" + entry.getValue());
        }
        //V remove(Object key) | 删除对应的Key-Value键值对
        map.remove("world");
        System.out.println(map);
        //boolean containsKey(Object key)
        System.out.println(map.containsKey("world"));
        //boolean containsValue(Object value)
        System.out.println(map.containsValue(1));
    }
}
